package cn.easier.activity.console.controller;

import cn.easier.activity.console.configration.ActivityConsoleConfiguration;
import cn.easier.activity.console.service.ActivityAdvancedSettingService;
import cn.easier.activity.console.service.ActivityInfoService;
import cn.easier.activity.console.utils.FileUtil;
import cn.easier.activity.web.entity.ActivityAdvancedSetting;
import cn.easier.activity.web.entity.ActivityInfo;
import cn.easier.activity.web.entity.PrizeClawImageMessage;
import cn.easier.club.base.dto.ResultInfo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

/**
 * 活动高级设置Controller
 */
@Controller
@RequestMapping("/activity/activityInfoId/{activityInfoId}/advanced-setting")
public class ActivityAdvancedSettingController extends BaseController {

    Logger logger = LoggerFactory.getLogger(ActivityAdvancedSettingController.class);

    @Autowired
    ActivityInfoService activityInfoService;

    @Autowired
    ActivityAdvancedSettingService activityAdvancedSettingService;

    @Autowired
    ActivityConsoleConfiguration activityConsoleConfiguration;

    /**
     * 创建抓娃娃活动高级设置
     *
     * @return
     */
    @PostMapping(value = "/savePrizeClawImageMessage")
    @ResponseBody
    public ResultInfo savePrizeClawImageMessage(@PathVariable Integer activityInfoId,
                                                @RequestParam(value = "bigBackgroundImage", required = true) MultipartFile bigBackgroundImage,
                                                @RequestParam(value = "littleBackgroundImage", required = true) MultipartFile littleBackgroundImage,
                                                @RequestParam(value = "horseRaceLampImage", required = true) MultipartFile horseRaceLampImage,
                                                @RequestParam(value = "DismantlingDollsImage", required = true) MultipartFile DismantlingDollsImage,
                                                @RequestParam(value = "FrameImage", required = true) MultipartFile FrameImage,
                                                @RequestParam(value = "participationRecordImage", required = true) MultipartFile participationRecordImage,
                                                @RequestParam(value = "activityRuleImage", required = true) MultipartFile activityRuleImage,
                                                @RequestParam(value = "startButtonImage", required = true) MultipartFile startButtonImage,
                                                @RequestParam(value = "advancedButtonImage", required = true) MultipartFile advancedButtonImage,
                                                @RequestParam(value = "primaryButtonImage", required = true) MultipartFile primaryButtonImage,
                                                @RequestParam(value = "dollStyle", required = true) String dollStyle) {
        PrizeClawImageMessage prizeClawImageMessage = new PrizeClawImageMessage();
        String prefix = "wap_";
        String relativePath = activityConsoleConfiguration.activityAdvancedSettingWapImagePath;
        if (bigBackgroundImage != null && (!Strings.isNullOrEmpty(bigBackgroundImage.getOriginalFilename()))) {
            String fileName = bigBackgroundImage.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            prizeClawImageMessage.setBigBackgroundImage(uploadFile(bigBackgroundImage, relativePath, "background_image_" + prefix));
        }
        if (littleBackgroundImage != null && (!Strings.isNullOrEmpty(littleBackgroundImage.getOriginalFilename()))) {
            String fileName = littleBackgroundImage.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            prizeClawImageMessage.setLittleBackgroundImage(uploadFile(littleBackgroundImage, relativePath, "turntable_style_" + prefix));
        }
        if (horseRaceLampImage != null && (!Strings.isNullOrEmpty(horseRaceLampImage.getOriginalFilename()))) {
            String fileName = horseRaceLampImage.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            prizeClawImageMessage.setHorseRaceLampImage(uploadFile(horseRaceLampImage, relativePath, "slogan_" + prefix));
        }
        if (DismantlingDollsImage != null && (!Strings.isNullOrEmpty(DismantlingDollsImage.getOriginalFilename()))) {
            String fileName = DismantlingDollsImage.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            prizeClawImageMessage.setDismantlingDollsImage(uploadFile(DismantlingDollsImage, relativePath, "prize_icon_" + prefix));
        }
        if (FrameImage != null && (!Strings.isNullOrEmpty(FrameImage.getOriginalFilename()))) {
            String fileName = FrameImage.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            prizeClawImageMessage.setFrameImage(uploadFile(FrameImage, relativePath, "prize_icon_" + prefix));
        }
        if (participationRecordImage != null && (!Strings.isNullOrEmpty(participationRecordImage.getOriginalFilename()))) {
            String fileName = participationRecordImage.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            prizeClawImageMessage.setParticipationRecordImage(uploadFile(participationRecordImage, relativePath, "prize_icon_" + prefix));
        }
        if (activityRuleImage != null && (!Strings.isNullOrEmpty(activityRuleImage.getOriginalFilename()))) {
            String fileName = activityRuleImage.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            prizeClawImageMessage.setActivityRuleImage(uploadFile(activityRuleImage, relativePath, "prize_icon_" + prefix));
        }
        if (startButtonImage != null && (!Strings.isNullOrEmpty(startButtonImage.getOriginalFilename()))) {
            String fileName = startButtonImage.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            prizeClawImageMessage.setStartButtonImage(uploadFile(startButtonImage, relativePath, "prize_icon_" + prefix));
        }
        if (advancedButtonImage != null && (!Strings.isNullOrEmpty(advancedButtonImage.getOriginalFilename()))) {
            String fileName = advancedButtonImage.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            prizeClawImageMessage.setAdvancedButtonImage(uploadFile(advancedButtonImage, relativePath, "prize_icon_" + prefix));
        }
        if (primaryButtonImage != null && (!Strings.isNullOrEmpty(primaryButtonImage.getOriginalFilename()))) {
            String fileName = primaryButtonImage.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            prizeClawImageMessage.setPrimaryButtonImage(uploadFile(primaryButtonImage, relativePath, "prize_icon_" + prefix));
        }
        prizeClawImageMessage.setDollStyles(dollStyle);
        ResultInfo message = activityInfoService.savePrizeClawImageMessage(activityInfoId, prizeClawImageMessage);
        return message;
    }

    /**
     * 查找指定活动高级设置列表
     *
     * @return
     */
    @GetMapping
    @ResponseBody
    public ResultInfo findList(@PathVariable Integer activityInfoId) {
        //todo 如果当前用户不存在，返回异常
        ResultInfo resultInfo = null;
        String user = getPrincipal().getUsername();
        String authoritiesStr = getPrincipal().getAuthoritiesStr();
        ActivityInfo activityInfo = getActivityInfo(authoritiesStr, user, activityInfoId);
        //查看关联的活动，如果此活动属于当前用户，根据活动编号获取指定规则。
        if (activityInfo == null) {
            logger.error("获取活动高级设置列表失败，当前用户不拥有指定活动对象。user: =>{},activityInfoId:=>{}", user, activityInfoId);
            return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "获取活动高级设置列表失败", null);
        }
        List<ActivityAdvancedSetting> activityAdvancedSettings = activityAdvancedSettingService.findByActivityInfoId(activityInfoId);
        resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "SUCCESS", activityAdvancedSettings);
        logger.info("获取活动高级设置列表成功：=>{}", activityAdvancedSettings);
        return resultInfo;
    }

    /**
     * 查找指定活动指定设置
     *
     * @return
     */
    @GetMapping("/scene/{sceneStr}")
    @ResponseBody
    public ResultInfo findOne(@PathVariable Integer activityInfoId, @PathVariable String sceneStr) {
        //todo 如果当前用户不存在，返回异常
        String user = getPrincipal().getUsername();
        String authoritiesStr = getPrincipal().getAuthoritiesStr();
        ResultInfo resultInfo = null;
        ResultInfo readyScene = readyScene(sceneStr);
        if (!readyScene.getStatus().equals("200") && !readyScene.getDescription().equals("SUCCESS")) {
            return readyScene;
        }
        //查看关联的活动，如果此活动属于当前用户，根据活动编号获取指定规则。
        ActivityInfo activityInfo = getActivityInfo(authoritiesStr, user, activityInfoId);
        if (activityInfo == null) {
            logger.error("获取活动高级设置失败，当前用户不拥有指定活动对象。user: =>{},activityInfoId:=>{}", user, activityInfoId);
            return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "获取活动高级设置失败", null);

        }
        //关联活动Id,定位具体活动
        ActivityAdvancedSetting activityAdvancedSetting = activityAdvancedSettingService.findOne(Integer.valueOf(readyScene.getData().toString()), activityInfoId);
        resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "SUCCESS", activityAdvancedSetting);
        logger.info("获取活动高级设置成功：=>{}", activityAdvancedSetting);
        return resultInfo;
    }

    /**
     * 创建活动高级设置
     *
     * @return
     */
    @PostMapping
    @ResponseBody
    public ResultInfo create(
            @PathVariable Integer activityInfoId, ActivityAdvancedSetting activityAdvancedSetting,
            @RequestParam(value = "backgroundImageFile", required = false) MultipartFile backgroundImage,
            @RequestParam(value = "turntableStyleFile", required = false) MultipartFile turntableStyle,
            @RequestParam(value = "sloganFile", required = false) MultipartFile sloganImage,
            @RequestParam(value = "prizeIconFile", required = false) MultipartFile prizeIcon,
            @RequestParam(value = "topic1File", required = false) MultipartFile topic1,
            @RequestParam(value = "topic2File", required = false) MultipartFile topic2,
            @RequestParam(value = "redEnvelopesFile", required = false) MultipartFile redEnvelopes,
            @RequestParam(value = "redEnvelopes1File", required = false) MultipartFile redEnvelopesImg,
            @RequestParam(value = "redEnvelopes2File", required = false) MultipartFile redEnvelopesImgOne,
            @RequestParam(value = "redEnvelopes3File", required = false) MultipartFile redEnvelopesImgTwo,
            @RequestParam(value = "redEnvelopes4File", required = false) MultipartFile redEnvelopesImgThree,
            @RequestParam(value = "eggTwisting1File", required = false) MultipartFile eggTwistingOne,
            @RequestParam(value = "eggTwisting2File", required = false) MultipartFile eggTwistingTwo,
            @RequestParam(value = "eggTwisting3File", required = false) MultipartFile eggTwistingThree) {
        String user = getPrincipal().getUsername();
        String authoritiesStr = getPrincipal().getAuthoritiesStr();
        ResultInfo resultInfo = null;
        try {
            ActivityInfo activityInfo = getActivityInfo(authoritiesStr, user, activityInfoId);
            //查看关联的活动，如果此活动属于当前用户，根据活动编号获取指定规则。
            if (activityInfo == null) {
                logger.error("创建活动高级设置异常，当前用户不拥有指定活动对象。user: =>{},activityInfoId:=>{}", user, activityInfoId);
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "创建活动高级设置异常", null);
            }
            //如果存在该活动对应的适应环境，则不能重复添加
            Integer scene = activityAdvancedSetting.getScene();
            if (scene != 0 && scene != 1) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "创建活动高级设置异常，适应环境不合法", null);
            }
            ActivityAdvancedSetting activityAdvancedSettingScene = activityAdvancedSettingService.findOne(scene, activityInfoId);
            if (activityAdvancedSettingScene != null) {
                logger.error("创建活动高级设置异常，当前用户已经拥有对应的适应环境。user: =>{},activityInfoId:=>{},scene:=>{}", user, activityInfoId, scene == 0 ? "wap" : "pc");
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "创建活动高级设置异常,已经存在" + (scene == 0 ? "wap" : "pc") + "环境高级设置", null);
            }

            String prefix = "";
            String relativePath = "";
            //适用环境：0：wap 1:pc
            if (scene == 0) {
                prefix = "wap_";
                relativePath = activityConsoleConfiguration.activityAdvancedSettingWapImagePath;
            } else if (scene == 1) {
                prefix = "pc_";
                relativePath = activityConsoleConfiguration.activityAdvancedSettingPcImagePath;
            } else {
                logger.error("创建活动高级设置异常，没有选择适应环境。user: =>{},activityInfoId:=>{}", user, activityInfoId);
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "创建活动高级设置异常，没有选择适应环境", null);
            }
            //图片上传
            activityAdvancedSetting.setActivityInfoId(activityInfoId);
            ResultInfo readyUploadFile = readyUploadFile(activityAdvancedSetting, backgroundImage, turntableStyle, sloganImage, prizeIcon, redEnvelopes, redEnvelopesImg, redEnvelopesImgOne, redEnvelopesImgTwo, redEnvelopesImgThree, eggTwistingOne, eggTwistingTwo, eggTwistingThree, prefix, relativePath);
            if (!readyUploadFile.getStatus().equals("200")) {
                return readyUploadFile;
            }
            // 把数据存到高级配置里
            String advancedSetting = activityInfo.getAdvancedSetting();
            JSONObject advancedConfigJson;
            if (StringUtils.isNotBlank(advancedSetting)) {
                advancedConfigJson = JSON.parseObject(advancedSetting);
            } else {
                advancedConfigJson = new JSONObject();
            }
            ResultInfo readyUploadTopicFile = readyUploadTopicFile(advancedConfigJson, topic1, topic2, prefix, relativePath);
            if (!readyUploadTopicFile.getStatus().equals("200")) {
                return readyUploadTopicFile;
            }
            advancedConfigJson = (JSONObject) readyUploadTopicFile.getData();
            activityInfo.setAdvancedSetting(advancedConfigJson.toJSONString());
            activityInfoService.save(activityInfo);
            ActivityAdvancedSetting returnActivityAdvancedSetting = activityAdvancedSettingService.save((ActivityAdvancedSetting) readyUploadFile.getData());
            logger.info("创建活动高级设置成功：=>{}", returnActivityAdvancedSetting);
            resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "SUCCESS", returnActivityAdvancedSetting);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("创建活动高级设置失败，activityInfoId：=>{}", activityInfoId);
            resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "创建活动高级设置异常", null);
        }
        return resultInfo;
    }

    /**
     * 创建活动高级设置
     *
     * @return
     */
    @PutMapping
    @ResponseBody
    public ResultInfo update(
            @PathVariable Integer activityInfoId,
            ActivityAdvancedSetting activityAdvancedSetting,
            @RequestParam(value = "backgroundImageFile", required = false) MultipartFile backgroundImage,
            @RequestParam(value = "turntableStyleFile", required = false) MultipartFile turntableStyle,
            @RequestParam(value = "sloganImageFile", required = false) MultipartFile sloganImage,
            @RequestParam(value = "prizeIconFile", required = false) MultipartFile prizeIcon,
            @RequestParam(value = "topic1File", required = false) MultipartFile topic1,
            @RequestParam(value = "topic2File", required = false) MultipartFile topic2,
            @RequestParam(value = "redEnvelopesFile", required = false) MultipartFile redEnvelopes,
            @RequestParam(value = "redEnvelopes1File", required = false) MultipartFile redEnvelopesImg,
            @RequestParam(value = "redEnvelopes2File", required = false) MultipartFile redEnvelopesImgOne,
            @RequestParam(value = "redEnvelopes3File", required = false) MultipartFile redEnvelopesImgTwo,
            @RequestParam(value = "redEnvelopes4File", required = false) MultipartFile redEnvelopesImgThree,
            @RequestParam(value = "eggTwistingOne", required = false) MultipartFile eggTwistingOne,
            @RequestParam(value = "eggTwistingTwo", required = false) MultipartFile eggTwistingTwo,
            @RequestParam(value = "eggTwistingThree", required = false) MultipartFile eggTwistingThree) {
        ResultInfo resultInfo = null;
        String user = getPrincipal().getUsername();
        String authoritiesStr = getPrincipal().getAuthoritiesStr();
        if (activityAdvancedSetting.getId() == null || activityInfoId != activityAdvancedSetting.getActivityInfoId()) {
            logger.error("更新活动高级设置异常，信息不合法。user: =>{},activityInfoId:=>{}", user, activityInfoId);
            return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "更新活动高级设置异常，信息不合法", null);
        }
        String prefix = "";
        String relativePath = "";
        //适用环境：0：wap 1:pc
        if (activityAdvancedSetting.getScene() == 0) {
            prefix = "wap_";
            relativePath = activityConsoleConfiguration.activityAdvancedSettingWapImagePath;
        } else if (activityAdvancedSetting.getScene() == 1) {
            prefix = "pc_";
            relativePath = activityConsoleConfiguration.activityAdvancedSettingPcImagePath;
        } else {
            logger.error("更新活动高级设置异常，没有选择适应环境。user: =>{},activityInfoId:=>{}", user, activityInfoId);
            return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "更新活动高级设置异常，没有选择适应环境", null);
        }
        try {
            //查看关联的活动，如果此活动属于当前用户，根据活动编号获取指定规则。
            ActivityInfo activityInfo = getActivityInfo(authoritiesStr, user, activityInfoId);
            if (activityInfo == null) {
                logger.error("更新活动高级设置异常，当前用户不拥有指定活动对象。user: =>{},activityInfoId:=>{}", user, activityInfoId);
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "更新活动高级设置异常", null);
            }
            //图片上传
            ResultInfo readyUploadFile = readyUploadFile(activityAdvancedSetting, backgroundImage, turntableStyle, sloganImage, prizeIcon, redEnvelopes, redEnvelopesImg, redEnvelopesImgOne, redEnvelopesImgTwo, redEnvelopesImgThree, eggTwistingOne, eggTwistingTwo, eggTwistingThree, prefix, relativePath);
            if (!readyUploadFile.getStatus().equals("200")) {
                return readyUploadFile;
            }
            // 把数据存到高级配置里
            String advancedSetting = activityInfo.getAdvancedSetting();
            JSONObject advancedConfigJson;
            if (StringUtils.isNotBlank(advancedSetting)) {
                advancedConfigJson = JSON.parseObject(advancedSetting);
            } else {
                advancedConfigJson = new JSONObject();
            }
            ResultInfo readyUploadTopicFile = readyUploadTopicFile(advancedConfigJson, topic1, topic2, prefix, relativePath);
            if (!readyUploadTopicFile.getStatus().equals("200")) {
                return readyUploadTopicFile;
            }
            advancedConfigJson = (JSONObject) readyUploadTopicFile.getData();
            activityInfo.setAdvancedSetting(advancedConfigJson.toJSONString());
            activityInfoService.save(activityInfo);
            ActivityAdvancedSetting returnActivityAdvancedSetting = activityAdvancedSettingService.update((ActivityAdvancedSetting) readyUploadFile.getData());
            logger.info("更新活动高级设置成功：=>{}", returnActivityAdvancedSetting);
            resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "SUCCESS", returnActivityAdvancedSetting);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("更新活动高级设置失败，activityInfoId：=>{}", activityInfoId);
            resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "更新活动高级设置异常", null);
        }
        return resultInfo;
    }

    /**
     * 删除指定活动指定设置
     *
     * @return
     */
    @DeleteMapping("/scene/{sceneStr}")
    @ResponseBody
    public ResultInfo delete(@PathVariable Integer activityInfoId, @PathVariable String sceneStr) {
        //todo 如果当前用户不存在，返回异常
        String user = getPrincipal().getUsername();
        String authoritiesStr = getPrincipal().getAuthoritiesStr();
        ResultInfo resultInfo = null;
        ResultInfo readyScene = readyScene(sceneStr);
        if (!readyScene.getStatus().equals("200") && !readyScene.getDescription().equals("SUCCESS")) {
            return readyScene;
        }
        try {
            //查看关联的活动，如果此活动属于当前用户，根据活动编号获取指定规则。
            ActivityInfo activityInfo = getActivityInfo(authoritiesStr, user, activityInfoId);
            if (activityInfo == null) {
                logger.error("删除活动高级设置失败，当前用户不拥有指定活动对象。user: =>{},activityInfoId:=>{}", user, activityInfoId);
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "删除活动高级设置失败，当前用户不拥有指定活动对象", null);
            }
            //关联活动Id,定位具体活动
            int changeNum = activityAdvancedSettingService.delete(Integer.valueOf(readyScene.getData().toString()), activityInfoId);
            logger.info("删除高级设置,打印删除数据量： activityInfoId =>{}，changeNum =>{}", activityInfoId, changeNum);
            boolean deleteFlag = changeNum > 0 ? true : false;
            resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "SUCCESS", deleteFlag);
            logger.info("删除高级设置： activityInfoId =>{}，flag =>{}", activityInfoId, deleteFlag ? "SUCCESS" : "No Change");
        } catch (Exception e) {
            logger.info("删除高级设置异常： activityInfoId =>{}", activityInfoId);
            resultInfo = new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "删除活动高级设置异常", null);
        }
        return resultInfo;
    }

    private ResultInfo readyScene(String sceneStr) {
        Integer scene;
        if ("wap".equals(sceneStr)) {
            scene = 0;
        } else if ("pc".equals(sceneStr)) {
            scene = 1;
        } else {
            return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "获取活动高级设置失败，适应环境不合法", null);
        }
        return new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "SUCCESS", scene);
    }

    private ResultInfo readyUploadFile(ActivityAdvancedSetting activityAdvancedSetting, MultipartFile backgroundImage, MultipartFile turntableStyle,
                                       MultipartFile sloganImage, MultipartFile prizeIcon, MultipartFile redEnvelopes, MultipartFile redEnvelopesImg,
                                       MultipartFile redEnvelopesImgOne, MultipartFile redEnvelopesImgTwo, MultipartFile redEnvelopesImgThree, MultipartFile eggTwistingOne, MultipartFile eggTwistingTwo, MultipartFile eggTwistingThree, String prefix, String relativePath) {
        if (backgroundImage != null && (!Strings.isNullOrEmpty(backgroundImage.getOriginalFilename()))) {
            String fileName = backgroundImage.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            activityAdvancedSetting.setBackgroundImage(uploadFile(backgroundImage, relativePath, "background_image_" + prefix));
        }
        if (turntableStyle != null && (!Strings.isNullOrEmpty(turntableStyle.getOriginalFilename()))) {
            String fileName = turntableStyle.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            activityAdvancedSetting.setTurntableStyle(uploadFile(turntableStyle, relativePath, "turntable_style_" + prefix));
        }
        if (sloganImage != null && (!Strings.isNullOrEmpty(sloganImage.getOriginalFilename()))) {
            String fileName = sloganImage.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            activityAdvancedSetting.setSlogan(uploadFile(sloganImage, relativePath, "slogan_" + prefix));
        }
        if (prizeIcon != null && (!Strings.isNullOrEmpty(prizeIcon.getOriginalFilename()))) {
            String fileName = prizeIcon.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            activityAdvancedSetting.setPrizeIcon(uploadFile(prizeIcon, relativePath, "prize_icon_" + prefix));
        }
        if (redEnvelopes != null && (!Strings.isNullOrEmpty(redEnvelopes.getOriginalFilename()))) {
            String fileName = redEnvelopes.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            activityAdvancedSetting.setRedEnvelopes(uploadFile(redEnvelopes, relativePath, "redEnvelopesFile_" + prefix));
        }
        if (redEnvelopesImg != null && (!Strings.isNullOrEmpty(redEnvelopesImg.getOriginalFilename()))) {
            String fileName = redEnvelopesImg.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            activityAdvancedSetting.setRedEnvelopesImg(uploadFile(redEnvelopesImg, relativePath, "redEnvelopesFile1_" + prefix));
        }
        if (redEnvelopesImgOne != null && (!Strings.isNullOrEmpty(redEnvelopesImgOne.getOriginalFilename()))) {
            String fileName = redEnvelopesImgOne.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            activityAdvancedSetting.setRedEnvelopesImgOne(uploadFile(redEnvelopesImgOne, relativePath, "redEnvelopesFile2_" + prefix));
        }
        if (redEnvelopesImgTwo != null && (!Strings.isNullOrEmpty(redEnvelopesImgTwo.getOriginalFilename()))) {
            String fileName = redEnvelopesImgTwo.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            activityAdvancedSetting.setRedEnvelopesImgTwo(uploadFile(redEnvelopesImgTwo, relativePath, "redEnvelopesFile3_" + prefix));
        }
        if (redEnvelopesImgThree != null && (!Strings.isNullOrEmpty(redEnvelopesImgThree.getOriginalFilename()))) {
            String fileName = redEnvelopesImgThree.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            activityAdvancedSetting.setRedEnvelopesImgThree(uploadFile(redEnvelopesImgThree, relativePath, "redEnvelopesFile4_" + prefix));
        }
        if (eggTwistingOne != null && (!Strings.isNullOrEmpty(eggTwistingOne.getOriginalFilename()))) {
            String fileName = eggTwistingOne.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            activityAdvancedSetting.setEggTwistingOne(uploadFile(eggTwistingOne, relativePath, "eggTwistingOne_" + prefix));
        }
        if (eggTwistingTwo != null && (!Strings.isNullOrEmpty(eggTwistingTwo.getOriginalFilename()))) {
            String fileName = eggTwistingTwo.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            activityAdvancedSetting.setEggTwistingTwo(uploadFile(eggTwistingTwo, relativePath, "eggTwistingTwo_" + prefix));
        }
        if (eggTwistingThree != null && (!Strings.isNullOrEmpty(eggTwistingThree.getOriginalFilename()))) {
            String fileName = eggTwistingThree.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            activityAdvancedSetting.setEggTwistingThree(uploadFile(eggTwistingThree, relativePath, "eggTwistingThree_" + prefix));
        }
        return new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "SUCCESS", activityAdvancedSetting);
    }

    private ResultInfo readyUploadTopicFile(JSONObject advancedConfigJson, MultipartFile topic1, MultipartFile topic2, String prefix, String relativePath) {
        if (topic1 != null && (!Strings.isNullOrEmpty(topic1.getOriginalFilename()))) {
            String fileName = topic1.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            if (prefix.startsWith("pc")) {
                advancedConfigJson.put("topic1Image_pc", uploadFile(topic1, relativePath, "topic1_" + prefix));
            } else {
                advancedConfigJson.put("topic1Image_wap", uploadFile(topic1, relativePath, "topic1_" + prefix));
            }
        }
        if (topic2 != null && (!Strings.isNullOrEmpty(topic2.getOriginalFilename()))) {
            String fileName = topic2.getOriginalFilename();
            //验证图片文件格式
            boolean imageFormatFlag = FileUtil.imageFormatVerification(fileName);
            if (!imageFormatFlag) {
                return new ResultInfo(String.valueOf(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), "请选择格式为*.jpg、*.gif、*.bmp、*.png、*.jpeg 的图片", fileName);
            }
            if (prefix.startsWith("pc")) {
                advancedConfigJson.put("topic2Image_pc", uploadFile(topic2, relativePath, "topic2_" + prefix));
            } else {
                advancedConfigJson.put("topic2Image_wap", uploadFile(topic2, relativePath, "topic2_" + prefix));
            }
        }
        return new ResultInfo(String.valueOf(HttpServletResponse.SC_OK), "SUCCESS", advancedConfigJson);
    }

    private String uploadFile(MultipartFile imageFile, String relativePath, String fileNamePrefix) {
        //根路径
        String realPath = activityConsoleConfiguration.realPath;
        //绝对路径
        String absolutePath = (realPath + relativePath).replace("/", File.separator);
        String imageFileName = FileUtil.doFile(imageFile, absolutePath, fileNamePrefix);
        //静态资源映射 + 文件地址 + 上传文件名称
        return relativePath + imageFileName;
    }

}
